package com.pay.ioopos.fragment;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.pay.ioopos.KeyCodeFactory;
import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.R;
import com.pay.ioopos.activity.DomainActivity;


public class ConnectFragment extends AbstractFragment implements View.OnKeyListener {
    private View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null != view) {
            view.requestFocus();
            return view;
        }
        view = inflater.inflate(R.layout.fragment_connect, container, false);
        view.setFocusable(true);
        view.setOnKeyListener(this);
        view.requestFocus();
        return view;
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() != KeyEvent.ACTION_UP) {
            return true;
        }

        KeyInfo keyInfo = KeyCodeFactory.getKeyInfo(keyCode);
        if (null == keyInfo) {
            return true;
        }

        switch (keyInfo) {
            case KEY_NUM_1:
                setMainFragment(new NetInfoFragment());
                return true;
            case KEY_NUM_2:
                setMainFragment(new NetStatFragment());
                return true;
            case KEY_NUM_3:
                setMainFragment(new NetCableMenuFragment());
                return true;
            case KEY_NUM_4:
                setMainFragment(new NetWifiMenuFragment());
                return true;
            case KEY_NUM_5:
                setMainFragment(new Net4GMenuFragment());
                return true;
            case KEY_NUM_6:
                startActivity(new Intent(getContext(), DomainActivity.class));
                return true;
            case KEY_NUM_7:
                setMainFragment(new QueryPeriodFragment());
                return true;
        }

        return false;
    }


    private void enableWifi(boolean bool) {
        WifiManager wifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        wifiManager.setWifiEnabled(bool);
    }

    private void enable4G(boolean bool) {
        if (bool) {
            Intent intent = new Intent("com.snstar.networkparameters.DATA_OPEN");
            getContext().sendBroadcast(intent);
        } else {
            Intent intent = new Intent("com.snstar.networkparameters.DATA_CLOSE");
            getContext().sendBroadcast(intent);
        }
    }

    private void enableCable(boolean bool) {
        if (bool) {
            Intent intent = new Intent("com.snstar.networkparameters.ETH_OPEN");
            getContext().sendBroadcast(intent);
        } else {
            Intent intent = new Intent("com.snstar.networkparameters.ETH_CLOSE");
            getContext().sendBroadcast(intent);
        }
    }

    private void dhcp() {
        Intent intent = new Intent("com.snstar.networkparameters.ETH_DHCP");
        getContext().sendBroadcast(intent);
    }
}
